#include "game_control.h"
#include "print.h"

void custom_timer(int n, void (*callback)(UINT, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR))
{
	DWORD_PTR dwUser = NULL;
	// ���ö�ʱ��
	int timerID = timeSetEvent(n, 1, (LPTIMECALLBACK)callback, dwUser, TIME_PERIODIC);
}
int move_left(int n, int m)
{
	// 1.左边界检测
	if (dynamic_x <= 12)
	{
		return 1;
	}
	// 2.左碰撞检测
	if (check_shape(n, m, dynamic_x - 2, dynamic_y))
	{
		return 1;
	}
	// 3.向左移动
	erase_last_shape(n, m, dynamic_x, dynamic_y);
	dynamic_x -= 2;
	print_mode_shape(n, m, dynamic_x, dynamic_y, dynamic_color);

	return 0;
}

int move_right(int n, int m)
{
	// 1.右边界检测
	if (dynamic_x + 2 * (4 - shape[n][m][16]) - 1 >= 39)
	{
		return 1;
	}
	// 2.左碰撞检测
	if (check_shape(n, m, dynamic_x + 2, dynamic_y))
	{
		return 1;
	}
	// 3.向左移动
	erase_last_shape(n, m, dynamic_x, dynamic_y);
	dynamic_x += 2;
	print_mode_shape(n, m, dynamic_x, dynamic_y, dynamic_color);

	return 0;
}

// 变形
int change_shape()
{
	int m = (dynamic_mode + 1) % 4;
	// 右边越界判定
	if (dynamic_x + 2 * (4 - shape[dynamic_num][m][16]) - 1 > 39)
	{
		return 1;
	}
	// 下边越界判定
	if (dynamic_y + (4 - shape[dynamic_num][m][17] - 1) > 29)
	{
		return 1;
	}

	erase_last_shape(dynamic_num, dynamic_mode, dynamic_x, dynamic_y);
	dynamic_mode = m;
	print_mode_shape(dynamic_num, dynamic_mode, dynamic_x, dynamic_y, dynamic_color);
	return 0;
}

void fall_down()
{
	int touch_down = 0;
	while (1)
	{
		touch_down = move_down(dynamic_num, dynamic_mode);
		if (touch_down == 1)
		{
			return;
		}
	}
}

int move_down(int num, int mode)
{
	if ((dynamic_y + (4 - shape[num][mode][17]) - 1 >= 29)
		|| check_shape(num, mode, dynamic_x, dynamic_y + 1))
	{
		store_current_shape();

		init_new_shape();

		// 消行
		eliminate_line();

		print_next_shape();
		return 1;
	}
	erase_last_shape(num, mode, dynamic_x, dynamic_y);
	dynamic_y++;
	print_mode_shape(num, mode, dynamic_x, dynamic_y, dynamic_color);
	return 0;
}

// 定时器回调函数
void timer_callback(UINT uTimerId, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
{
	// 每次往下移动
	move_down(dynamic_num, dynamic_mode);

	if (check_game_over() == 1)
	{
		game_over();
		exit(0); // 结束当前进程
	}
}

// 绘制从二维矩阵拿到的行
void print_matrix()
{
	int i = 0, j = 0;
	for (i = 0; i < 24; i++)
	{
		for (j = 0; j < 28; j += 2)
		{
			if (matrix[i][j] == 0)
			{
				printf("\033[%d;%dH  \033[0m", i + 6, j + 12);
			}
			else
			{
				printf("\033[%d;%dH\033[%dm[]\033[0m", i + 6, j + 12, matrix[i][j]);
			}
		}
	}
	return;
}

// 打印score level
void print_score_level()
{
	printf("\33[%d;%dHScore:%d\33[0m", score_y, score_x, score);
	printf("\33[%d;%dHLevel:%d\33[0m", level_y, level_x, level);
	fflush(NULL);
}

void eliminate_line()
{
	int i = 0, j = 0, k = 0;
	// flag定义某一行是否已经填满方格,0未填满 1已填满
	int flag = 0;

	for (i = 0; i < 24; i++)
	{
		flag = 1;
		for (j = 0; j < 28; j++)
		{
			if (matrix[i][j] == 0)
			{
				flag = 0;
				break;
			}
		}

		// 如果已填满
		if (flag == 1)
		{
			score += 50;
			if (score % 100 == 0)
			{
				level++;
				ms /= 2;
				// 重新设置定时器
				custom_timer(ms, timer_callback);
			}
			// 删除已填满的行,整体下移
			for (k = i; k > 0; k--)
			{
				for (j = 0; j < 28; j++)
				{
					matrix[k][j] = matrix[k - 1][j];
				}
			}
			print_matrix();
			print_score_level();
		}
	}
}

void user_control()
{
	int ch;
	while (1)
	{
		ch = getch();
		
		// 输入q Q Esc退出 
		if (ch == 113 || ch == 81 || ch == 27)
		{
			break;
		}

		switch (ch)
		{
		case 13: // 输入Enter 快速下落
			fall_down();
			continue;
		case 72: // 方向上键
			change_shape();
			continue;
		case 80: // 方向下键
			move_down(dynamic_num, dynamic_mode);
			continue;
		case 75: // 方向左键
			move_left(dynamic_num, dynamic_mode);
			continue;
		case 77: // 方向右键
			move_right(dynamic_num, dynamic_mode);
			continue;
		default:
			continue;
		}
	}

	game_over();
	return;
}